package com.doudou.collection.infrastructure.common.regex;

import com.doudou.collection.infrastructure.common.ex.ServiceException;
import com.doudou.collection.infrastructure.common.web.StatusCode;
import com.doudou.collection.utils.regex.RegexUtils;
import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName LogInAndRegValidate<br>
 * @Description 格式验证<br>
 * @Author 李浩文<br>
 * @Date 2023/6/15 15:51<br>
 * @Version 1.0<br>
 */
@Slf4j
public class FormatValidate {

    private static void usernameFormatValidate(String username) {
        //判断用户名格式是否正确
        if (RegexUtils.isUsernameInvalid(username)) {
            log.error("该用户名格式错误！ ===> {}", username);
            throw new ServiceException(StatusCode.USERNAME_FORMAT_ERROR);
        }
    }

    private static void passwordFormatValidate(String password) {
        //判断密码格式是否正确
        if (RegexUtils.isPasswordInvalid(password)) {
            log.error("该密码格式错误！ ===> {}", password);
            throw new ServiceException(StatusCode.PASSWORD_FORMAT_ERROR);
        }
    }

    private static void phoneFormatValidate(String phone) {
        //手机号码格式是否正确
        if (RegexUtils.isPhoneInvalid(phone)) {
            log.error("手机号码格式错误！ ===> {}", phone);
            throw new ServiceException(StatusCode.PHONE_FORMAT_ERROR);
        }
    }

    private static void emailFormatValidate(String email) {
        //邮箱格式是否正确
        if (RegexUtils.isEmailInvalid(email)) {
            log.error("邮箱格式错误！ ===> {}", email);
            throw new ServiceException(StatusCode.EMAIL_FORMAT_ERROR);
        }
    }

    private static void codeFormatValidate(String code) {
        //验证码格式是否正确
        if (RegexUtils.isUsernameInvalid(code)) {
            log.error("该验证码格式错误！ ===> {}", code);
            throw new ServiceException(StatusCode.CODE_FORMAT_ERROR);
        }
    }
}
